<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../layui/css/layui.css" media="all">
</head>
<body>
<fieldset class="layui-elem-field layui-field-title">
    <legend>应用上传</legend>
</fieldset>
<form class="layui-form layui-form-pane"  id="form" enctype="multipart/form-data" >
    <div class="layui-form-item">
        <label class="layui-form-label">应用名称</label>
        <div class="layui-input-block">
            <input type="text" name="yymc" lay-verify="required" autocomplete="off" placeholder="请输入应用名称"
                   class="layui-input">
        </div>
    </div>

    <!--<div class="layui-form-item">-->
    <!--<label class="layui-form-label">封面图片</label>-->
    <!--<div class="layui-input-block">-->
    <!--<div class="layui-upload">-->
    <!--<button type="button" class="layui-btn" id="fm">上传图片</button>-->
    <!--<blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px">-->
    <!--预览图：-->
    <!--<div class="layui-upload-list" id="fm_img"></div>-->
    <!--<p id="fm_p"></p>-->
    <!--</blockquote>-->
    <!--</div>-->
    <!--</div>-->
    <!--</div>-->

    <!--<div class="layui-form-item">-->
    <!--<label class="layui-form-label">预览图片</label>-->
    <!--<div class="layui-input-block">-->
    <!--<div class="layui-upload">-->
    <!--<button type="button" class="layui-btn" id="yl">上传图片</button>-->

    <!--<blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">-->
    <!--预览图：-->
    <!--<div class="layui-upload-list" id="yl_img"></div>-->
    <!--</blockquote>-->
    <!--</div>-->
    <!--</div>-->
    <!--</div>-->


    <div class="layui-form-item">
        <label class="layui-form-label">开发单位</label>
        <div class="layui-input-block">
            <input type="text" name="kfdw" lay-verify="required" autocomplete="off" placeholder="请输入开发单位"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">负责人</label>
        <div class="layui-input-block">
            <input type="text" name="fzr" lay-verify="required" autocomplete="off" placeholder="请输入负责人"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">上传APK</label>
        <div class="layui-input-inline">
            <input type="file" id="file" name="file" lay-verify="required" placeholder="请输入" autocomplete="off"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">说明</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入内容" name="sm" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <button class="layui-btn" lay-submit="" lay-filter="demo1">跳转式提交</button>
    </div>
</form>

<script src="../../layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.use(['form', 'layedit', 'laydate', 'upload'], function () {
        var $ = layui.jquery;
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , upload = layui.upload
            , laydate = layui.laydate;

        var uploadInst = upload.render({
            elem: '#fm'
            , url: '/fileUpload/upload'
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#fm_img').html('<img  style="width: 100px;margin-right: 10px" src="' + result + '" alt="' + file.name + '" class="layui-upload-img">')

                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                //上传成功
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#fm_p');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });

        var uploadInsts = upload.render({
            elem: '#yl'
            , url: '/fileUpload/uploadFiles'
            , multiple: true
            , before: function (obj) {

                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#yl_img').append('<img  style="width: 100px;margin-right: 10px" src="' + result + '" alt="' + file.name + '" class="layui-upload-img">')
                });
            }

            , allDone: function (obj) { //当文件全部被提交后，才触发
                console.log(obj.total); //得到总文件数
                console.log(obj.successful); //请求成功的文件数
                console.log(obj.aborted); //请求失败的文件数
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#yl_p');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInsts.upload();
                });
            }
        });

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });

        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');

        //自定义验证规则
        form.verify({
            title: function (value) {
                if (value.length < 5) {
                    return '标题至少得5个字符啊';
                }
            }
            , pass: [/(.+){6,12}$/, '密码必须6到12位']
            , content: function (value) {
                layedit.sync(editIndex);
            }
        });

        //监听指定开关
        form.on('switch(switchTest)', function (data) {
            layer.msg('开关checked：' + (this.checked ? 'true' : 'false'), {
                offset: '6px'
            });
            layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
        });

        //监听提交
        form.on('submit(demo1)', function (data) {

            var formData = new FormData($("#form")[0]);//用form 表单直接 构造formData 对象; 就不需要下面的append 方法来为表单进行赋值了。
            // var formData = new FormData();
            // formData.append('file', $('#file')[0].files[0]); // 固定格式
            //
            // formData.append('yymc',data.field.yymc);
            // formData.append('kfdw',data.field.kfdw);
            // formData.append('fzr',data.field.fzr);
            // formData.append('sm',data.field.sm);

            // layer.alert(JSON.stringify(data.field), {
            //     title: '最终的提交信息'
            // })

            $.ajax({
                type: 'post',
                url: '/fileUpload/apkUpload',
                data: {
                    formData: formData
                },
                /**
                 *必须false才会自动加上正确的Content-Type
                 */
                contentType: false,
                processData: false,
                success: function (data) {
                    alert("success");
                },
                error: function (error) {
                    alert(error);
                }

            });
            return false;
        });

        //表单初始赋值
        form.val('example', {
            "username": "贤心" // "name": "value"
            , "password": "123456"
            , "interest": 1
            , "like[write]": true //复选框选中状态
            , "close": true //开关状态
            , "sex": "女"
            , "desc": "我爱 layui"
        })


    });
</script>
</body>
</html>